//
//  ZFCustomCell.swift
//  mapbox
//
//  Created by fyzq on 2024/1/3.
//

import Foundation
import RxSwift

class ZFCustomCell :UIView {
    
    private(set) var titleLabel : UILabel!
    private(set) var arrow : UIImageView!
    private(set) var rightStackView : UIStackView!
    private(set) var leftStackView : UIStackView!
    private var stackView : UIStackView!
    var extData:Any?
    var insets:UIEdgeInsets = UIEdgeInsets.zero {
        didSet{
            stackView.snp.updateConstraints { make in
                make.edges.equalToSuperview().inset(insets)
            }
            self.layoutIfNeeded()
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setZFCustomCell()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func setZFCustomCell(){

        titleLabel = UILabel.init()
        titleLabel.font = UIFont.boldSystemFont(ofSize: 13)
        titleLabel.text = ""
        titleLabel.textColor = R.color.color_00()
        
        
        leftStackView = UIStackView(arrangedSubviews: [titleLabel])
        leftStackView.axis = .horizontal
        leftStackView.alignment = .center
        leftStackView.distribution = .equalCentering
        leftStackView.spacing = 5
        
        
        arrow = UIImageView();
        arrow.image = R.image.ic_svg_arrow_right()
        arrow.contentMode = UIView.ContentMode.scaleAspectFit

        
        rightStackView = UIStackView(arrangedSubviews: [arrow])
        rightStackView.axis = .horizontal
        rightStackView.alignment = .center
        rightStackView.distribution = .fillProportionally
        rightStackView.spacing = 0
        
        
        stackView = UIStackView(arrangedSubviews: [leftStackView,rightStackView])
        stackView.axis = .horizontal
        stackView.alignment = .fill
        stackView.distribution = .equalSpacing
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.height.equalTo(40)
            make.edges.equalToSuperview()
        }
    }
}

class ZFContentCustomCell : ZFCustomCell {
    var contentLabel:UILabel!
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentLabel =  UILabel()
        contentLabel.textColor = R.color.color_00()
        contentLabel.font = UIFont.systemFont(ofSize: 13)
        self.rightStackView.insertArrangedSubview(contentLabel, at: 0)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class ZFIconCustomCell : ZFContentCustomCell {
    var iconLabel:UILabel!
    override init(frame: CGRect) {
        super.init(frame: frame)
        iconLabel = UILabel.init()
        iconLabel.font = UIFont.iconfont(size: 22)
        iconLabel.textColor = R.color.color_00()
        self.leftStackView.insertArrangedSubview(iconLabel, at: 0)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
class ZFImageCustomCell : ZFContentCustomCell {
    var imageView:UIImageView!
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 30));
        imageView.image = UIImage.init(named: "ic_setting_default_header");
        imageView.contentMode = UIView.ContentMode.scaleAspectFit
        self.leftStackView.insertArrangedSubview(imageView, at: 0)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class ZFSwitchCustomCell : ZFImageCustomCell {
    var switchBar:UISwitch!
    override init(frame: CGRect) {
        super.init(frame: frame)
        switchBar =  UISwitch()
        switchBar.onTintColor = R.color.accentColor()
        switchBar.tintColor = UIColor.init(white: 1, alpha: 0.6)
        switchBar.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
        self.imageView.isHidden = true
        self.arrow.isHidden = true
        self.rightStackView.insertArrangedSubview(switchBar, at: 1)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}



class ZFGroupCustomCell :UIStackView {


    var showSpace:Bool = false /// 是否显示分割线
    override init(frame: CGRect) {
        super.init(frame: frame)
        setZFGroupCustomCell()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func setZFGroupCustomCell(){
        self.axis = .vertical
        self.alignment = .fill
        self.distribution = .equalSpacing
        self.spacing = 1
    }
    
    func addChilds(list:[UIView]){
        
        list.forEach({[weak self] item in
            guard let self = self else {return}
            self.addArrangedSubview(item)
        })
        
        list.forEach { item in
            
        }
        
    }
    

}

